package org.lqq.util.sql;


import org.lqq.util.sql.enums.SqlOperatorEnum;

/**
 * @author liangqq
 */
public abstract class SqlParamUtil {


    /**
     * 拼接where参数，如果value不为空，那么拼接条件，否则不拼接
     * @param sqlContainerSb sql容器
     * @param column 列明
     * @param operator 操作符
     * @param value 字段的值
     */
    public static <T> void addParam(StringBuilder sqlContainerSb, String column, SqlOperatorEnum operator, T value){
        if(value == null){
            return;
        }
        String stringValue;
        if(value instanceof Boolean){
            Boolean b = (Boolean)value;
            stringValue = b ? "1" : "0";
        }else{
            stringValue = value.toString();
        }

        if(stringValue == null || stringValue.length() == 0){
            return;
        }

        switch (operator){
            case EQUAL:
            case GREATER_THAN_EQUAL:
            case LESS_THAN_EQUAL:
                sqlContainerSb
                    .append(SqlOperatorEnum.SPACE.operator)
                    .append(SqlOperatorEnum.AND.operator).append(SqlOperatorEnum.SPACE.operator)
                    .append(column).append(SqlOperatorEnum.SPACE.operator)
                    .append(operator.operator).append(SqlOperatorEnum.SPACE.operator)
                    .append(SqlOperatorEnum.SINGLE_QUOTE.operator)
                    .append(stringValue)
                    .append(SqlOperatorEnum.SINGLE_QUOTE.operator);
                break;
            case LIKE:
                sqlContainerSb
                    .append(SqlOperatorEnum.SPACE.operator)
                    .append(SqlOperatorEnum.AND.operator).append(SqlOperatorEnum.SPACE.operator)
                    .append(column).append(SqlOperatorEnum.SPACE.operator)
                    .append(operator.operator).append(SqlOperatorEnum.SPACE.operator)
                    .append("'%")
                    .append(stringValue)
                    .append("%'")
                ;
                break;
            default:
                throw new RuntimeException("不识别的操作符" + operator);

        }


    }
}
